package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhRoleMapper;
import com.oracle.pojo.PhRole;
import com.oracle.pojo.PhRoleExample;
import com.oracle.pojo.PhRoleExample.Criteria;
import com.oracle.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private PhRoleMapper roleMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhRole> findAll() {
		return roleMapper.selecPhyExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhRole> page=   (Page<PhRole>) roleMapper.selecPhyExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhRole role) {
		roleMapper.insert(role);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhRole role){
		roleMapper.updateByPrimaryKey(role);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhRole findOne(Integer id){
		return roleMapper.selecPhyPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] ids) {
		for(Integer id:ids){
			roleMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhRole role, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhRoleExample example=new PhRoleExample();
		Criteria criteria = example.createCriteria();

		if(role!=null){
						if(role.getRolename()!=null && role.getRolename().length()>0){
				criteria.andRolenameLike("%"+role.getRolename()+"%");
			}
		}

		Page<PhRole> page= (Page<PhRole>)roleMapper.selecPhyExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
